<template>

	<view class="home">
		<!-- 导航 -->
		<view class="">
			<u-navbar placeholder :safeAreaInsetTop="true" title="站点管理" :fixed="true" left-text="返回" @leftClick="leftClick">
				<view class="u-nav-slot" slot="right">
					<u-icon name="plus" size="24" :bold="true" @click="showpop = true"></u-icon>
				</view>
			</u-navbar>
		</view>
		<mescroll-body @init="mescrollInit" @up="upCallback" :down="downOption" @down="downCallback" :up="upOption">
			<uni-swipe-action v-for="(i,index) in dataList" :key="index">
				<uni-swipe-action-item  v-if="show1" :right-options="options1" @click="clickswipe($event, i)">
					<view class="devlist" >
						<image class="img" src="@/static/images/shuiwji.png" mode=""></image>
						<view class="devitem">
							<view class="text">
								{{i.projectName}}
							</view>
						</view>
						<view class="" style="font-size: 30rpx;color: #b0b0b0;">
							滑动管理
						</view>

					</view>
				</uni-swipe-action-item>
				</uni-swipe-action>
		</mescroll-body>
		<!-- 弹窗 -->
		<u-popup :closeOnClickOverlay="false" :show="showpop" safeAreaInsetTop mode="top" :round="10">
			<view class="pop">
				<view class="" style="text-align: center;font-size: 50rpx;color: #000000;margin-bottom: 50rpx;">
					创建站点
				</view>
				<view class="" style="display: flex;align-items: center;">
					<view class="">
						站点名称：
					</view>
					<u--input placeholder="请输入站点名称" border="bottom" v-model="projectName"
						clearable></u--input>
				</view>
				<u-button @tap="$u.debounce(btnAClick, 300)" style="margin: 30rpx 0;" type="primary"
					text="确定"></u-button>
				<u-button @click="cancellation" text="取消"></u-button>
			</view>
		</u-popup>
		<!-- 删除提示 -->
		<u-modal :show="showdele" showCancelButton @confirm="confirmdele" @cancel="canceldele">
			<view class="">
				
				<text>是否删除 </text>
				<text style="font-size: 50rpx;color: #ff5457;">{{contentdele}}</text>
				<text> 站点</text>
			</view>
		</u-modal>
		<u-modal :show="modifyshowdele" showCancelButton @confirm="modifyconfirm" @cancel="modifycancel">
			<view class="">
				<view class="" style="font-size: 50rpx;color: #478f6a;text-align: center;">
					{{contentdele}}
				</view>
				<view class="" style="display: flex;align-items: center;">
					<view class="">
						站点名称：
					</view>
					<u--input placeholder="请输入站点名称" border="bottom" v-model="projectName"
						clearable></u--input>
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import MescrollBodyDiy from "@/uni_modules/mescroll-uni/components/mescroll-diy/beibei/mescroll-body.vue";
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		components: {
			MescrollBodyDiy // 避免与main.js注册的全局组件名称相同,否则注册组件失效(iOS真机 APP HBuilderX2.7.9)
		},
		data() {
			return {
				showdele: false,
				modifyshowdele: false,
				contentdele: '',
				show1: true,
				upOption: {
					page: {
						num: 0,
						size: 10 // 每页数据的数量,默认10
					},
					// noMoreSize: 2,
					empty: {
						tip: '暂无相关数据'
					}
				},
				downOption: {},
				dataList: [],
				showpop: false,
				projectName: '',
				options1: [{
					text: '删除',
					style: {
						backgroundColor: '#eb4c4c'
					}
				},{
					text: '修改',
					style: {
						backgroundColor: '#ffaa00'
					}
				}],
				projectID:null,
				modifyName:null,
				modifyID:'',
			}
		},
		onLoad(option) {
			// let page = {
			// 	num: 1,
			// 	size: 10
			// }
			// this.upCallback(page)
		},
		methods: {
			// 滑动数据
			clickswipe(id,e) {
				console.log('滑动',id);
				if(id.content.text == '删除'){
					this.contentdele = e.projectName
					this.projectID = e.id
					this.showdele = true
				}
				if(id.content.text == '修改'){
					this.contentdele = e.projectName
					this.projectID = e.id
					this.projectName = ''
					this.modifyshowdele = true
				}
				
			},
			// 确定
			confirmdele(){
					console.log('确定');
					this.deleteremove()
				this.showdele = false
			},
			// 取消
			canceldele(){
				this.showdele = false
			},
			// 删除接口
			deleteremove(){
				uni.$u.http.delete('/device/project/remove/'+ this.projectID ).then(res => {
					console.log('res', res);
					if (res.code == 200) {
				this.downCallback() 
					} else {
						uni.showToast({
							title: '删除失败',
							duration: 1500
						});
					}
				}).catch(err => {
					console.log('错误', err);
				})
			},
			// 修改确定
			modifyconfirm(){
					console.log('确定');
					this.modifyedit()
				this.modifyshowdele = false
			},
			// 修改取消
			modifycancel(){
				this.modifyshowdele = false
			},
			// 修改接口
			modifyedit(){
				uni.$u.http.put('/device/project/edit',{
					id:this.projectID,
					projectName:this.projectName
				}).then(res => {
					console.log('res', res);
					if (res.code == 200) {
				this.downCallback() 
					} else {
						uni.showToast({
							title: '修改失败',
							duration: 1500
						});
					}
				}).catch(err => {
					console.log('错误', err);
				})
			},
			// 弹窗
			// 确定
			btnAClick() {
				if (this.projectName == '') {
					uni.showToast({
						title: '名称不能为空',
						duration: 1500
					});
					return
				}
				this.showpop = false
				uni.$u.http.post('/device/project/add', {
					"projectName": this.projectName
				}).then(res => {
					if (res.code == 200) {
						this.downCallback()
					} else {
						uni.showToast({
							title: '创建站点失败',
							duration: 1500
						});
					}
				}).catch(err => {
					console.log('错误', err);
				})
			},
			// 取消
			cancellation() {
				this.showpop = false
			},
			// 返回
			leftClick() {
				uni.navigateBack({
					delta: 1
				});
			},

			// 设备列表
			upCallback(page) {
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条
				uni.$u.http.get('/device/project/list', {
					params: {
						"pageSize": pageSize,
						"pageNum": pageNum,
					}
				}).then(res => {
					if (res.code == 200) {

						// 接口返回的当前页数据列表 (数组)
						let curPageData = res.rows;
						// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
						let curPageLen = curPageData.length;
						// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
						// let totalPage = data.total;
						// 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
						let totalSize = res.total;
						// 接口返回的是否有下一页 (true/false)

						//设置列表数据
						if (page.num == 1) this.dataList = []; //如果是第一页需手动置空列表
						this.dataList = this.dataList.concat(curPageData); //追加新数据

						//方法二(推荐): 后台接口有返回列表的总数据量 totalSize
						this.mescroll.endBySize(curPageLen, totalSize);
					} else {
						uni.showToast({
							title: '暂无数据',
							duration: 1500
						});
						uni.reLaunch({
							url: '/pages/home/login'
						});
					}
				}).catch(err => {
					console.log('err', err);
					//  请求失败,隐藏加载状态
					this.mescroll.endErr()
				})
			},
			// 下拉刷新
			downCallback() {
				this.dataList = []
				this.mescroll.resetUpScroll();
			},
			/*若希望重新加载列表,只需调用此方法即可(内部会自动page.num=1,再主动触发up.callback)*/
			reloadList() {
				this.mescroll.resetUpScroll();
			}
		}
	}
</script>
<style scoped lang="scss">
	.home {
		padding: 0 30rpx 30rpx 30rpx;

		.devlist {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 15rpx;
			margin: 15rpx;
			border-radius: 20rpx;
			background-color: #fafbfd;
			border-bottom: 2rpx solid #d7d7d8;

			.img {
				width: 80rpx;
				height: 80rpx;
				border-radius: 10rpx;
				margin: 10rpx;
			}

			.devitem {
				display: flex;
				flex-direction: column;
				justify-content: center;

				.nick {
					// text-align: center;
					// border-radius: 10rpx;
					font-size: 40rpx;
					color: #100f23;
					// margin-bottom: 10rpx;
					// background-color: #3c557f;
				}

				.text {
					// text-align: center;
					font-size: 40rpx;
					// margin: 15rpx;
					color: #1a1a1b;
				}
			}

		}
	}

	// 弹窗
	.pop {
		padding: 150rpx 50rpx 50rpx 50rpx;
	}
</style>
<style>
	page {
		background-color: #ffffff;
	}
</style>